package com.ysc.ArrangeClass;

import com.ysc.domain.Class;
import com.ysc.utils.mybatisUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;
import java.util.Scanner;

public class Verify {

    //添加新班级
    public static void main(String[] args){
        int a = 0;
        SqlSession sqlSession = mybatisUtils.getSqlSession();
        String SqlId;
        System.out.println("请输入新的班级信息：");

        Scanner in = new Scanner(System.in);
        Class cla = new Class();

        //对比Cid看是否有重复的
        SqlId = "com.ysc.Dao.ClassDao.JudgeIfCidExist";
        while(a == 0){
            System.out.println("请输入班级号：");
            int cid = in.nextInt();
            cla.setCid(cid);
            int num = sqlSession.selectOne(SqlId,cla);
            if (num != 0) {
                System.out.println("班级号重复，请更换");
            }
            else{
                a=1;
            }
        }

        //对比Tid和CTime是否有重复的
        a = 0;
        String Ctime;
        SqlId = "com.ysc.Dao.ClassDao.JudgeIfTidandCTimeExist";
        while(a == 0){
            System.out.println("请输入教师号：");
            int tid = in.nextInt();
            cla.setTid(tid);
            System.out.println("请输入上课时间：");
            Ctime = in.next();
            cla.setCTime(Ctime);
            int num = sqlSession.selectOne(SqlId,cla);
            if (num != 0) {
                System.out.println("该教师同一时间在其他班级有课，请更换");
            }
            else{
                a=1;
            }
        }

        //对比CPlace和CTime是否有重复的
        a = 0;
        SqlId = "com.ysc.Dao.ClassDao.JudgeIfCPlaceandCTimeExist";
        while(a == 0){
            System.out.println("上课地点：");
            String CPlace = in.next();
            cla.setCPlace(CPlace);
            int num = sqlSession.selectOne(SqlId,cla);
            if (num != 0) {
                System.out.println("该教室该时间段有其他班级上课，请更换");
            }
            else{
                a=1;
            }
        }

        System.out.println("上课周期：");
        cla.setCPeriod(in.next());
        cla.setCNum(0);

        SqlId = "com.ysc.Dao.ClassDao.insertClass";
        sqlSession.insert(SqlId,cla);
        sqlSession.commit();
        sqlSession.close();
        System.out.println("班级添加完成！");
    }
}
